package com.dawn.module.hospital.controller.app.jop;

import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.quartz.core.handler.JobHandler;
import com.dawn.framework.tenant.core.job.TenantJob;
import com.dawn.module.hospital.controller.app.register.vo.RegisterUnLockReqVO;
import com.dawn.module.hospital.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 * 解锁号源任务
 *
 * @author mamba 4 left
 */
@Slf4j
@Component
public class UnLockRegNumberJob implements JobHandler {

    @Resource
    private RegisterService registerService;

    @Override
    @TenantJob
    public String execute(String param) {
         Long ofMinutes = 0L;
         Long endMinutes = 0L;
        if(param != null){
            try {
                JSONObject object = JSONObject.parseObject(param);
                ofMinutes = object.getLongValue("ofMinutes");
                endMinutes = object.getLongValue("endMinutes");
            }catch (Exception e){

            }
        }

        try {
            RegisterUnLockReqVO unLockReqVO = new RegisterUnLockReqVO();
            Duration duration = Duration.ofMinutes(5);

            if (ofMinutes > 0) {
                duration = Duration.ofMinutes(ofMinutes);
            }

            LocalDateTime nowTime = LocalDateTime.now();
            unLockReqVO.setBeginTime(nowTime.minus(duration));

            duration = Duration.ofMinutes(2);
            if (endMinutes > 0) {
                duration = Duration.ofMinutes(endMinutes);
            }
            unLockReqVO.setEndTime(nowTime.minus(duration));
            registerService.unLockRegNumber(unLockReqVO);
        } catch (Exception e) {
            log.error("解锁号源异常", e);
        }
        return "解锁号源完成";
    }
}
